package cn.dt67.service.impl;

import cn.dt67.dao.user.UserMapper;
import cn.dt67.pojo.User;
import cn.dt67.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    private static final Logger logger = Logger.getLogger(UserServiceImpl.class);

    //@Override
    public List<User> getAllUser() {
        List<User> userList = null;
        try {
            userList = userMapper.findAll();
            if(userList == null || userList.size() <= 0){
                logger.debug("UserServiceImpl.getAllUser 查询结果为空！");
                return new ArrayList<User>();//避免前台报错，空指针
            }
        } catch (Exception e) {
//            e.printStackTrace();
            logger.error("UserServiceImpl.getAllUser 查询结果异常！"+e.getMessage());
        }
        logger.debug("UserServiceImpl.getAllUser 查询结果成功！");
        return userList;
    }

    //@Override
    public User findUser(String userCode, String userPassword) {
        User user = null;
        try {
           user = userMapper.findUser(userCode,userPassword);
           //初步筛选
            if(user == null){
                user = new User();
                logger.debug("UserServiceImpl.findUser 查询用户为空！");
            }
        } catch (Exception e) {
//            e.printStackTrace();
            logger.error("UserServiceImpl.findUser 查询用户信息异常！"+e.getMessage());
            user = new User();
        }
        return user;
    }

    //@Override
    public List<User> findUserList(String roleName, int roleId, int currentPageNo, int pageSize) {
        List<User> userList = null;
        int from = 0;
        try {
            from = (currentPageNo-1)*pageSize;
            userList = userMapper.findUserList(roleName,roleId,from,pageSize);
            if(userList == null || userList.size() <= 0){
                logger.debug("UserServiceImpl.findRoleList 查询结果为空！");
                return new ArrayList<User>();//避免前台报错，空指针
            }
        } catch (Exception e) {
//            e.printStackTrace();
            logger.error("UserServiceImpl.findRoleList 查询结果异常！"+e.getMessage());
        }
        logger.debug("UserServiceImpl.findRoleList 查询结果成功！");
        return userList;
    }

    //@Override
    public int getUserCount(String roleName, int roleId) {
        int count = 0;
        try {
            count = userMapper.findUserCount(roleName,roleId);
            if(count == 0){
                logger.debug("未查询到可用的用户记录 ！");
            }
        } catch (Exception e) {
//            e.printStackTrace();
            logger.error("UserServiceImpl.getUserCount 查询用户总数异常"+e.getMessage());
        }

        return count;
    }

    //@Override
    public boolean addNewUser(User user) {
        int row = 0;
        try {
            row = userMapper.save(user);
            if(row != 1){
                logger.debug("UserServiceImpl.addNewUser 保存用户信息失败");
                return false;
            }else{
                logger.debug("UserServiceImpl.addNewUser 保存用户信息成功");
            }
        } catch (Exception e) {
//            e.printStackTrace();
            logger.error("UserServiceImpl.addNewUser 保存用户信息异常"+e.getMessage());
            return false;
        }
        return true;
    }

    //@Override
    public User findById(int id) {
        User user = null;
        try {
            user = userMapper.findById(id);
            if(user == null){
                logger.debug("UserServiceImpl.findById 根据id查询用户信息失败");
                return user = new User();
            }
        } catch (Exception e) {
//            e.printStackTrace();
            logger.error("UserServiceImpl.findById 根据id查询用户信息异常"+e.getMessage());
            user = new User();
        }

        return user;
    }

    //@Override
    public boolean update(User user) {
        int row = 0;

        try {
            row = userMapper.update(user);
            if(row != 1){
                logger.debug("UserServiceImpl.update 根据id更新用户信息失败");
                return false;
            }
        } catch (Exception e) {
//            e.printStackTrace();
            logger.error("UserServiceImpl.update 根据id更新用户信息异常"+e.getMessage());
            return false;
        }

        return true;
    }

    //@Override
    public boolean delete(int id) {
        int row = 0;

        try {
            row = userMapper.delete(id);
            if(row != 1){
                logger.debug("UserServiceImpl.delete 根据id删除用户信息失败");
                return false;
            }
        } catch (Exception e) {
//            e.printStackTrace();
            logger.error("UserServiceImpl.delete 根据id删除用户信息异常"+e.getMessage());
            return false;
        }

        return true;
    }

    //@Override
    public boolean userCodeExists(String userCode) {
        User user = null;
        try {
            user = userMapper.userCodeExists(userCode);
            if(null == user){
                return false;
            }else{
                return true;
            }
        } catch (Exception e) {
//            e.printStackTrace();
            logger.error("UserServiceImpl.userCodeExists 校验userCode异常！"+e.getMessage());
            return true;
        }
    }

}
